package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfVideocallGroup;
import com.ocom.common.entity.card.EntityXfVideocallInfo;
import com.ocom.common.entity.card.EntityXfVideocallNo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.videocall.request.GetVideocallGroupRequest;
import com.ocom.common.request.videocall.request.SelectVideocallGroupRequest;
import com.ocom.common.request.videocall.response.SelectVideocallGroupResponse;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfVideocallGroup;
import com.ocom.payconfig.service.XfVideocallGroupService;
import com.ocom.payconfig.service.XfVideocallNoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;


@Slf4j
@Service
public class XfVideocallGroupServiceImpl extends ServiceImpl<IDaoXfVideocallGroup, EntityXfVideocallGroup> implements XfVideocallGroupService {

    @Autowired
    private IDaoXfVideocallGroup dao;

    @Autowired
    private XfVideocallNoService xfVideocallNoService;

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<SelectVideocallGroupResponse> getVideocallGroupByPer(Long companyId, SelectVideocallGroupRequest request) {
        QueryWrapper<EntityXfVideocallGroup> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyId);
        List<EntityXfVideocallGroup> groupList = this.list(queryWrapper);
        Map<String, EntityXfVideocallNo> xfVideocallNoMap = new HashMap<>();
        if (null != request.getPId() && StringUtils.isNotBlank(request.getOpenid())) {
            xfVideocallNoMap = xfVideocallNoService.getByComIdAndPIdAndOpenid(companyId, request.getPId(), request.getOpenid()).stream().collect(Collectors.toMap(EntityXfVideocallNo::getGroupId, Function.identity(), (data1, data2) -> data1));
        }
        Map<String, EntityXfVideocallNo> finalXfVideocallNoMap = xfVideocallNoMap;
        List<SelectVideocallGroupResponse> groupByPerResponses = groupList.stream().map(g -> new SelectVideocallGroupResponse(g.getGroupId(), g.getGroupName(), finalXfVideocallNoMap.getOrDefault(g.getGroupId(), new EntityXfVideocallNo()).getStatus())).collect(Collectors.toList());

        return groupByPerResponses;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfVideocallGroup> getVideocallGroup(Long companyId, Long merchId) {
        return dao.getVideocallGroup(companyId, merchId);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean addVideocallGroup(Long companyId, EntityXfVideocallGroup entity) {
        return this.save(entity);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean updateActiveNumber(Long companyId, String groupId, int activeNumber) {
        UpdateWrapper<EntityXfVideocallGroup> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id", companyId);

        updateWrapper.eq("group_id", groupId);

        updateWrapper.set("active_number", activeNumber);

        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public EntityXfVideocallGroup getById(Long companyId, Long id) {
        return this.getById(id);
    }
}
